package com.liao.badminton.handler.security;


import cn.hutool.core.util.ObjectUtil;
import com.liao.badminton.code.ResponseRest;
import com.liao.badminton.code.myenum.RoleCodeEnum;
import com.liao.badminton.entity.BadmintonGroup;
import com.liao.badminton.entity.SystemUsers;
import com.liao.badminton.entity.UserGroupRef;
import com.liao.badminton.redis.RedisEnum;
import com.liao.badminton.redis.RedisUtil;
import com.liao.badminton.code.util.JwtTokenUtils;
import com.liao.badminton.code.util.ResponseUtils;
import com.liao.badminton.service.BadmintonGroupService;
import com.liao.badminton.service.SystemUsersService;
import com.liao.badminton.service.UserGroupRefService;
import com.liao.badminton.vo.BadmintonGroupVo;
import com.liao.badminton.vo.SystemUsersVo;
import com.liao.badminton.vo.UserGroupRefVo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Resource
    private SystemUsersService systemUsersService;
    @Resource
    private BadmintonGroupService badmintonGroupService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        UserDetails jwtUser = (UserDetails) authentication.getPrincipal();

        SystemUsersVo systemUsersVo = systemUsersService.findByName(jwtUser.getUsername());
//        List<BadmintonGroupVo> groupByUserId = badmintonGroupService.findGroupByUserId(systemUsersVo.getId());
//        systemUsersVo.setGroupList(groupByUserId);
        JwtTokenUtils.setToken( response, systemUsersVo,jwtUser);
//        systemUsersVo.setPassword(null);
//        SecurityContextHolder.getContext().setAuthentication(authentication);
//        String token = JwtTokenUtils.createToken(jwtUser.getUsername(), false, jwtUser);
//        RedisUtil.toSaveRedis(RedisEnum.USER_LOGIN_NAME, jwtUser.getUsername(), true);
        // 返回创建成功的token
        // 但是这里创建的token只是单纯的token
        // 按照jwt的规定，最后请求的格式应该是 `token:Bearer `
//        response.setHeader(JwtTokenUtils.TOKEN_HEADER, JwtTokenUtils.TOKEN_PREFIX + token);
        ResponseUtils.out(response, ResponseRest.suc("login", systemUsersVo));

    }

}
